/** * Search-box.ai * Author: Simon-Pierre Boucher * Contact: contact@spboucher.ai * File: apps/web/app/api/research/[id]/route.ts * Description: GET — full snapshot of a research session (state + citations). */ import { NextResponse } from "next/server"; import { sessions } from "@search-box/db"; import { ResearchState } from "@search-box/research"; import { ensureMigrated } from "@/lib/runner"; export const runtime = "nodejs"; export const dynamic = "force-dynamic"; export async function GET( _req: Request, { params }: { params: Promise<{ id: string }> } ): Promise { await ensureMigrated(); const { id } = await params; const session = await sessions.get(id); if (!session) return NextResponse.json({ error: "not found" }, { status: 404 }); const state = new ResearchState(id); const snap = await state.snapshot(); const citations = snap.sources .filter((s) => s.citationIndex !== null) .sort((a, b) => (a.citationIndex ?? 0) - (b.citationIndex ?? 0)) .map((s) => ({ index: s.citationIndex as number, sourceId: s.id, url: s.url, title: s.title })); return NextResponse.json({ session, ...snap, citations }); }